graphic = {}
graphic.screenX = 0
graphic.screenY = 0
graphic.BACKGRUND = love.graphics.newImage("resources/background.png")
graphic.resourcesPO = {}

graphic.textEffects = {}

------------------------------------------------------
--[[ painting ]]--------------------------------------
------------------------------------------------------

function graphic.paintLayer1() --different layers of background
	engine.paintPicture(level.BACKGRUND)
end

function graphic.paintLayer2() -- all gameelements
	graphic.paintPO()
end

function graphic.paintLayer3() -- all graphical elements
	graphic.paintOnceAnimations()
	graphic.paintTexteffect()
end

function graphic.paintLayer4() -- user interface
	
end

function graphic.paintTexteffect()
	for _, element in ipairs(graphic.textEffects) do
		engine.paintText(element.text, element.center[1], element.center[2])
	end
end

function graphic.paintPO() --paint PHYSICAL_OBJECTS in the view of the player TODO: add view
	for _,row in ipairs(PHYSICAL_OBJECT) do 
		for _,collumn in ipairs(row) do
			for _,element in ipairs(collumn) do
					engine.paintGameelement(element)
					if element.currentState.name == "control" then tmp = element end
			end
		end
	end
	if tmp then engine.paintGameelement(tmp) end
end

-- draw once playing animations
function graphic.paintOnceAnimations()
	for _, a in ipairs(once_animations) do
		a[1]:draw(a[2],a[3])
	end
end
------------------------------------------------------
--[[ loading / creating ]]----------------------------
------------------------------------------------------

function graphic.loadPO(list) -- loads the sprites of all PHYSICAL_OBJECTS. NOTE Attention. 

	for name, object in pairs(list) do
		graphic.resourcesPO[object.name] = engine.splitUpSprite(object.sprite, object.width, object.height)
	end
end

function graphic.newTexteffect(_x, _y, _text, _speed, _duration)
	o ={
		text = _text,
		center = {_x,_y},
		width = engine.getWidthOfString(_text),
		height = engine.getHeightOfString(_text),
		speed = _speed,
		countdown = newCountdown(_duration)
	}
	table.insert(graphic.textEffects,o)
end

function graphic.updateTexteffect()
	i = 1
	while i <= #graphic.textEffects and graphic.textEffects[i] ~= nil do
		if graphic.textEffects[i].countdown:check() then -- TODO: replace true by time condition 
			graphic.textEffects[i].center[2] = graphic.textEffects[i].center[2] - graphic.textEffects[i].speed
			i = i + 1
		else
			table.remove(graphic.textEffects, i)
		end
		
	end
end

function graphic.loadTileset(_path, _tileSize)
	tilePicture_ = engine.loadPicture(_path)
	tilePictureWidth_ = engine.getWidthOfPicture(tilePicture_)
	tilePictureHeight_ = engine.getHeightOfPicture(tilePicture_)
	table_ = {}
	table_[0] = tilePicture_
	for i = 1,(tilePictureWidth_/_tileSize) do
		for ii = 1,(tilePictureHeight_/_tileSize) do
			table.insert(table_, love.graphics.newQuad((i -1) *  _tileSize, (ii-1) * _tileSize, _tileSize, _tileSize, tilePictureWidth_, tilePictureHeight_))
		end
	end
	return table_
end

function graphic.creatTileBatch(_tileset, _map)
	batch_ = love.graphics.newSpriteBatch(_tileset[0], 50 * 50)
	for i_, v_ in ipairs(_map) do
		for ii_, vv_ in ipairs(v_) do
			batch_:addq(_tileset[vv_+10], (i_-1)*32, (ii_-1)*32)
		end
	end
	return batch_
end
